package com.xckj.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xckj.dao.ProjectversionMapper;
import com.xckj.dto.ProjectversionDto;
import com.xckj.entity.Projectversion;
import com.xckj.service.ProjectversionService;
import com.xckj.utils.ReturnT;
import com.xckj.utils.SnowFlake;

/**
 * @description 项目版本号表
 * @author yanchf
 * @date 2024-01-06
 */
@Service
public class ProjectversionServiceImpl implements ProjectversionService {

	@Resource
	private ProjectversionMapper projectversionMapper;


	@Override
	public Object insert(Projectversion projectversion) {

		// valid
		if (projectversion == null) {
			return ReturnT.error("必要参数缺失");
        }
        projectversion.setId(SnowFlake.getID());
		projectversionMapper.insert(projectversion);
        return ReturnT.success();
	}


	@Override
	public Object delete(String projectPath) {
		int ret = projectversionMapper.delete(projectPath);
		return ret>0?ReturnT.success():ReturnT.error();
	}


	@Override
	public Object update(Projectversion projectversion) {
		int ret = projectversionMapper.update(projectversion);
		return ret>0?ReturnT.success():ReturnT.error();
	}


	@Override
	public Projectversion load(int id) {
		return projectversionMapper.load(id);
	}


	@Override
	public Map<String,Object> pageList(int offset, int pagesize) {
		List<Projectversion> pageList = projectversionMapper.pageList(offset, pagesize);
		int totalCount = projectversionMapper.pageListCount(offset, pagesize);
		// result
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pageList", pageList);
		result.put("totalCount", totalCount);
		return ReturnT.success(result);
	}

	@Override
	public Map<String, Object> allVersionList(int offset, int pagesize) {
		List<Projectversion> pageList = projectversionMapper.pageList(offset, pagesize);
		int totalCount = projectversionMapper.pageListCount(offset, pagesize);
		List<ProjectversionDto> results = new ArrayList<ProjectversionDto>();
		if(pageList != null) {
			for(Projectversion project : pageList) {
				ProjectversionDto dto = new ProjectversionDto(project.getId() + "", project.getVersion());
				results.add(dto);
			}
		}
		// result
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pageList", results);
		result.put("totalCount", totalCount);
		return ReturnT.success(result);
	}

}